package cn.iocoder.yudao.module.wms.controller.admin.productionorder;

import cn.iocoder.yudao.module.wms.controller.admin.pomain.vo.PomainsRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.productionorder.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.productionorder.ProductionOrderDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.productionorder.ProductionOrdersDO;
import cn.iocoder.yudao.module.wms.service.productionorder.ProductionOrderService;

@Tag(name = "管理后台 - 生产任务单主表")
@RestController
@RequestMapping("/wms/production-order")
@Validated
public class ProductionOrderController {

    @Resource
    private ProductionOrderService productionOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建生产任务单主表")
    @PreAuthorize("@ss.hasPermission('wms:production-order:create')")
    public CommonResult<Long> createProductionOrder(@Valid @RequestBody ProductionOrderSaveReqVO createReqVO) {
        return success(productionOrderService.createProductionOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产任务单主表")
    @PreAuthorize("@ss.hasPermission('wms:production-order:update')")
    public CommonResult<Boolean> updateProductionOrder(@Valid @RequestBody ProductionOrderSaveReqVO updateReqVO) {
        productionOrderService.updateProductionOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产任务单主表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:production-order:delete')")
    public CommonResult<Boolean> deleteProductionOrder(@RequestParam("id") Long id) {
        productionOrderService.deleteProductionOrder(id);
        return success(true);
    }

    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:production-order:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Long id) {
        productionOrderService.process(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得生产任务单主表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:production-order:query')")
    public CommonResult<ProductionOrderRespVO> getProductionOrder(@RequestParam("id") String id) {
        try {
            ProductionOrderDO productionOrder = productionOrderService.getProductionOrder(Long.valueOf(id));
            return success(BeanUtils.toBean(productionOrder, ProductionOrderRespVO.class));
        }catch (Exception e)
        {
            ProductionOrderDO productionOrder = productionOrderService.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(productionOrder, ProductionOrderRespVO.class));
        }

    }

    @GetMapping("/page")
    @Operation(summary = "获得生产任务单主表分页")
    @PreAuthorize("@ss.hasPermission('wms:production-order:query')")
    public CommonResult<PageResult<ProductionOrderRespVO>> getProductionOrderPage(@Valid ProductionOrderPageReqVO pageReqVO) {
        PageResult<ProductionOrderDO> pageResult = productionOrderService.getProductionOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductionOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产任务单主表 Excel")
    @PreAuthorize("@ss.hasPermission('wms:production-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductionOrderExcel(@Valid ProductionOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductionOrderDO> list = productionOrderService.getProductionOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产任务单主表.xls", "数据", ProductionOrderRespVO.class,
                        BeanUtils.toBean(list, ProductionOrderRespVO.class));
    }
    @GetMapping("/findProductionOrderOperateList")
    @Operation(summary = "获取需要入库的生产订单")
    @PreAuthorize("@ss.hasPermission('wms:production-order:query')")
    public CommonResult<List<ProductionOrderRespVO>> findProductionOrderOperateList() {
        List<ProductionOrderDO> pageResult = productionOrderService.findProductionOrderOperateList();
        return success(BeanUtils.toBean(pageResult, ProductionOrderRespVO.class));
    }


    // ==================== 子表（生产任务单子） ====================

    @GetMapping("/production-orders/list-by-pid")
    @Operation(summary = "获得生产任务单子列表")
    @Parameter(name = "pid", description = "订单id")
    @PreAuthorize("@ss.hasPermission('wms:production-order:query')")
    public CommonResult<List<ProductionOrdersRespVO>> getProductionOrdersListByPid(@RequestParam("pid") Integer pid) {
        return success(productionOrderService.findProductionOrdersByPid(pid));
    }

    @GetMapping("/production-orders/operateList")
    @Operation(summary = "获得生产任务单子列表 用于入库等操作")
    @Parameter(name = "pid", description = "主表ID")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<List<ProductionOrdersRespVO>> operateList(@Valid ProductionOrdersDO productionOrdersDO) {
        return success(productionOrderService.findProductionOrdersOperateList(productionOrdersDO));
    }


    @GetMapping("/getProductionOrdersById")
    @Operation(summary = "获得采购订单子表详细信息")
    @Parameter(name = "id", description = "ID")
    @PreAuthorize("@ss.hasPermission('wms:pomain:query')")
    public CommonResult<ProductionOrdersRespVO> getProductionOrdersById(@RequestParam("id") Integer id) {
        return success(productionOrderService.findProductionOrdersById(id));
    }

}